package com.turnengine.client.local.message.bean;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * The Local Message.
 */
public class LocalMessage implements ILocalMessage, Comparable<ILocalMessage> {

	/** The header. */
	private ILocalMessageHeader header;
	/** The content. */
	private ILocalMessageContent content;

	/**
	 * Creates a new Local Message.
	 */
	public LocalMessage() {
	}

	/**
	 * Creates a new Local Message.
	 * @param header the header
	 * @param content the content
	 */
	public LocalMessage(ILocalMessageHeader header, ILocalMessageContent content) {
		setHeader(header);
		setContent(content);
	}

	/**
	 * Creates a new Local Message.
	 * @param localMessage the local message
	 */
	public LocalMessage(LocalMessage localMessage) {
		setHeader(localMessage.getHeader());
		setContent(localMessage.getContent());
	}

	/**
	 * Creates a new Local Message.
	 * @param iLocalMessage the i local message
	 */
	public LocalMessage(ILocalMessage iLocalMessage) {
		setHeader(iLocalMessage.getHeader());
		setContent(iLocalMessage.getContent());
	}

	/**
	 * Returns the header.
	 * @return the header.
	 */
	public ILocalMessageHeader getHeader() {
		return header;
	}

	/**
	 * Returns the content.
	 * @return the content.
	 */
	public ILocalMessageContent getContent() {
		return content;
	}

	/**
	 * Sets the header.
	 * @param header the header to set.
	 */
	public void setHeader(ILocalMessageHeader header) {
		this.header = header;
	}

	/**
	 * Sets the content.
	 * @param content the content to set.
	 */
	public void setContent(ILocalMessageContent content) {
		this.content = content;
	}

	/**
	 * Returns the hash code.
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		HashCodeBuilder hash = new HashCodeBuilder(839, 37);
		hash.append(header);
		hash.append(content);
		return hash.toHashCode();
	}

	/**
	 * Returns true if this is equal to the given object.
	 * @param object the object to compare.
	 * @return true if this is equal to the given object.
	 */
	@Override
	public boolean equals(Object object) {
		if (object == this) {
			return true;
		}
		if (object instanceof LocalMessage) {
			LocalMessage compare = (LocalMessage) object;
			EqualsBuilder equals = new EqualsBuilder();
			equals.append(this.header, compare.header);
			equals.append(this.content, compare.content);
			return equals.isEquals();
		}
		return false;
	}

	/**
	 * Compare this to the given object.
	 * @param compare the object to compare to.
	 * @return the result of the comparison.
	 */
	@Override
	public int compareTo(ILocalMessage compare) {
		CompareToBuilder builder = new CompareToBuilder();
		builder.append(this.header, compare.getHeader());
		builder.append(this.content, compare.getContent());
		return builder.toComparison();
	}

	/**
	 * Returns this as a string.
	 * @return this as a string.
	 */
	@Override
	public String toString() {
		ToStringBuilder string = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
		if (header != null) {
			string.append("header", header);
		}
		if (content != null) {
			string.append("content", content);
		}
		return string.toString();
	}
}
